/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons.tool;

import com.minecraft.moonlake.api.packet.wrapper.PacketPlayOutChat;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponAbstractPlayerInteractEntity;
import com.minecraft.moonlake.manager.RandomManager;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;

public class ThieveSword extends WeaponAbstractPlayerInteractEntity {

    public ThieveSword() {
        super("thievesword", "&b盗贼剑", Material.WOOD_SWORD);
    }

    @Override
    public String[] getLore() {
        return new String[] {
                "&7注: &8(&a一次性武器&8)",
                "&7右键目标玩家后摧毁背包 &a20% &7的物品"
        };
    }

    @Override
    protected void onInteract(PlayerInteractEntityEvent event, Player player, Entity entity) {
        // 处理玩家武器交互实体
        if(!(entity instanceof Player)) return; // 目标实体不是玩家则返回
        Player target = (Player) entity;
        player.setItemInHand(null); // 清除玩家手中武器
        playerUpdateInventory(player); // 更新背包

        List<Integer> indexContent = new ArrayList<>();
        ItemStack[] contents = target.getInventory().getContents();
        int contentLength = contents.length;

        for(int i = 0; i < contentLength * 0.2; i++)
            indexContent.add(RandomManager.getRandom().nextInt(contentLength));

        for(int i = 0; i < contentLength; i++) {
            ItemStack itemStack = contents[i];
            if(itemStack != null && itemStack.getType() != Material.AIR && indexContent.contains(i))
                contents[i] = null;
        }
        target.getInventory().setContents(contents);
        String message = getInstance().getMessage("ThieveSwordSuccessThieve");
        new PacketPlayOutChat(message, PacketPlayOutChat.Mode.HOTBAR).send(target);
    }
}
